/**
 * https://leetcode-cn.com/problems/move-zeroes/
 * 移动零
 * @param nums 
 */
const moveZeroes = (nums: number[]): void => {
    // 统计0的个数
    let count = 0;
    for (let i = 0; i < nums.length; i++) {
        if (nums[i] == 0) {
            // 个数加1
            count++;
        } else if (count != 0) {
            // i - 前面0的个数, 刚好是前面第一个0的下标
            nums[i - count] = nums[i];
            nums[i] = 0;
        }
    }
};


const moveZeroes2 = (nums: number[]): void => {
    let sorted = 0,
        // 从前往后找
        m = 0,
        len = nums.length;

    /**
     * 交换元素
     * @param l 
     * @param r 
     */
    const swap = (l: number, r: number) => {
        const tmp = nums[l];
        nums[l] = nums[r];
        nums[r] = tmp
    }

    while (m < len) {
        if (nums[m] !== 0) {
            // 与sorted位置交换
            swap(m, sorted++);
            while (sorted !== 0) sorted++
        }

        m++;
    }
};